import { ApiProperty } from '@midwayjs/swagger';
import { getSchema, Rule, RuleType } from '@midwayjs/validate';
import { alternatives, StrIdNullValidator, StrIdValidator } from '..';
import { DepartmentSchema, RoleSchema, UserSchema } from '../../schema';

export class SaveUserValidator implements UserSchema {
  @ApiProperty({
    type: 'string',
    title: '姓名',
    example: 'null',
    description: '姓名',
    required: true,
  })
  @Rule(RuleType.string().required())
  userName: string;

  @ApiProperty({
    type: 'string',
    title: '身份证号',
    example: 'null',
    description: '身份证号',
    required: true,
  })
  @Rule(RuleType.string().required())
  idCardNo: string;

  @ApiProperty({
    type: 'string',
    title: '手机号',
    example: 'null',
    description: '手机号',
    required: true,
  })
  @Rule(RuleType.string().required())
  mobile: string;

  @ApiProperty({
    type: 'string',
    title: '密码',
    example: 'null',
    description: '密码',
    required: true,
  })
  @Rule(RuleType.string().required())
  password: string;

  @ApiProperty({
    type: Array<StrIdValidator>,
    items: {
      type: StrIdValidator,
    },
    title: '用户角色',
    example: 'null',
    description: '用户角色',
    required: false,
  })
  @Rule(RuleType.array().items(getSchema(StrIdValidator)).allow())
  role?: RoleSchema[];

  @ApiProperty({
    type: StrIdValidator,
    title: '用户部门',
    example: 'null',
    description: '用户部门',
    required: true,
  })
  @Rule(getSchema(StrIdValidator).required())
  dept?: DepartmentSchema;

  @ApiProperty({
    type: 'string',
    title: '头像',
    example: 'null',
    description: '头像',
    required: false,
  })
  @Rule(RuleType.alternatives(alternatives))
  avatar?: string;

  @ApiProperty({
    type: 'string',
    title: '邮箱',
    example: 'null',
    description: '邮箱',
    required: false,
  })
  @Rule(RuleType.alternatives(alternatives))
  email?: string;
}

export class EditUserValidator implements Partial<UserSchema> {
  @ApiProperty({
    type: 'string',
    title: '姓名',
    example: 'null',
    description: '姓名',
    required: false,
  })
  @Rule(RuleType.alternatives(alternatives))
  userName?: string;

  @ApiProperty({
    type: 'string',
    title: '身份证号',
    example: 'null',
    description: '身份证号',
    required: false,
  })
  @Rule(RuleType.alternatives(alternatives))
  idCardNo?: string;

  @ApiProperty({
    type: 'string',
    title: '手机号',
    example: 'null',
    description: '手机号',
    required: false,
  })
  @Rule(RuleType.alternatives(alternatives))
  mobile?: string;

  @ApiProperty({
    type: Array<StrIdValidator>,
    items: {
      type: StrIdValidator,
    },
    title: '用户角色',
    example: 'null',
    description: '用户角色',
    required: false,
  })
  @Rule(RuleType.array().items(getSchema(StrIdValidator)).allow())
  role?: RoleSchema[];

  @ApiProperty({
    type: StrIdNullValidator,
    title: '用户部门',
    example: 'null',
    description: '用户部门',
    required: false,
  })
  @Rule(getSchema(StrIdNullValidator).allow())
  dept?: DepartmentSchema;

  @ApiProperty({
    type: 'string',
    title: '头像',
    example: 'null',
    description: '头像',
    required: false,
  })
  @Rule(RuleType.alternatives(alternatives))
  avatar?: string;

  @ApiProperty({
    type: 'string',
    title: '邮箱',
    example: 'null',
    description: '邮箱',
    required: false,
  })
  @Rule(RuleType.alternatives(alternatives))
  email?: string;
}
